// date-formatter.ts

export function formatDate(offset: number, type: string, format: string): string {
  const currentDate = new Date();
  const targetDate = new Date(currentDate);

  // 根据 offset 来计算目标日期
  targetDate.setDate(currentDate.getDate() + offset);

  const dict: any = {
    yyyy: targetDate.getFullYear(),
    MM: padZero(targetDate.getMonth() + 1),
    dd: padZero(targetDate.getDate()),
    HH: padZero(targetDate.getHours()),
    mm: padZero(targetDate.getMinutes()),
    ss: padZero(targetDate.getSeconds())
  };

  if (type === "start") {
    dict.HH = "00";
    dict.mm = "00";
    dict.ss = "00";
  } else if (type === "end") {
    dict.HH = "23";
    dict.mm = "59";
    dict.ss = "59";
  } else if (type === "time") {
    format = "HH:mm"; // 如果是 "time" 类型，强制格式化为 "HH:mm"
  }

  return format.replace(/(yyyy|MM?|dd?|HH?|mm?|ss?)/g, match => dict[match]);
}

function padZero(num: number): string {
  return num < 10 ? `0${num}` : `${num}`;
}
